package com.lvhx.springboot.callservice;

import java.lang.reflect.Method;

public class DefaultServiceExecutor implements IServiceExecutor {
    private IServiceProvider serviceProvider;

    public void setServiceProvider(IServiceProvider serviceProvider) {
        this.serviceProvider = serviceProvider;
    }

    @Override
    public <T> T execute(String serviceNmae, Object request) {
        Object service = this.serviceProvider.getService(serviceNmae);
        try{
            Method m = service.getClass().getMethod("execute",request.getClass());
            return (T) m.invoke(service,request);
        }catch (Throwable e){
            Throwable cause = e.getCause();
            if (cause instanceof Error){
                throw (Error)cause;
            }else if (cause instanceof RuntimeException){
                throw (RuntimeException)cause;
            }else {
                throw new RuntimeException("123");
            }
        }
    }
}
